package com.itheima.controller;

import com.itheima.common.AuditOpinionsResult;
import com.itheima.common.ResponseResult;
import com.itheima.common.SetRecordsResult;
import com.itheima.pojo.HmQuestions;
import com.itheima.pojo.Query;
import com.itheima.service.QuestionsService;
import com.itheima.service.UsersService;
import com.itheima.util.PageResult;
import com.itheima.util.QuestionAdd;
import com.itheima.util.QuestionsAddResult;
import com.itheima.util.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 题库管理
 */
@RestController
@RequestMapping("/questions")
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UsersService usersService;

    /**
     * 审核意见
     * @param id
     * @return
     */
    @GetMapping("/{id}/auditOpinions")
    public AuditOpinionsResult auditOpinions(@PathVariable Integer id){
        return questionsService.auditOpinions(id);
    }

    /**
     * 出题记录
     */
    @GetMapping("/{id}/setRecords")
     public SetRecordsResult setRecords(@PathVariable Integer id){
        return questionsService.setRecords(id);
    }

    /**
     * 基础题库列表
     */
    @GetMapping
    public PageResult findPage(Query query){
        //区分精选
        query.setIsChoiceness(null);
       return questionsService.findPage(query);
    }

    /**
     * 基础题库删除
     */
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable Integer id){
        return questionsService.deleteById(id);
    }

    /**
     * 基础题库添加
     */
    @PostMapping
    public QuestionsAddResult add(@RequestBody HmQuestions questionAdd){
        //补全创建人id
        String cookie = request.getHeader("Authorization");
        String[] split = cookie.split(" ");
        String s = split[1];
        List<Users> users = usersService.findById(s);
        for (Users user : users) {
            questionAdd.setCreatorID(user.getId());
        }
        return questionsService.add(questionAdd);
    }

    /**
     * 基础题库详情
     */
    @GetMapping("/{id}")
    public QuestionAdd findById(@PathVariable Integer id){
       return questionsService.findById(id);
    }

    /**
     * 基础题库修改
     */
    @PutMapping("/{id}")
    public ResponseResult modify(@RequestBody HmQuestions hmQuestions,@PathVariable Integer id){
        return questionsService.modify(hmQuestions);
    }

    /**
     * 批量导入题 todo 没有表 无法完成
     */

    /**
     * 精选题库上下架
     */
    @PostMapping("/choice/{id}/{publishState}")
    public ResponseResult modifyByPublishState(@PathVariable Integer id,@PathVariable Integer publishState){
        return questionsService.modifyByPublishState(id,publishState);
    }

    /**
     * 精选题库列表
     */
    @GetMapping("/choice")
    public PageResult findPagecchoice(Query query){
        if (null==query.getChkState()){
            //查询全部
            //封装数据,区分精选
            query.setIsChoiceness("精选");
        }
        return questionsService.findPage(query);
    }

    /**
     * 组题列表  todo 没有表 无法完成
     * 组题列表删除
     * 加入或移除精选
     */

    /**
     * 加入或移出精选
     */
    @PatchMapping("/choice/{id}/{choiceState}")
    public ResponseResult modifyByChoiceState(@PathVariable Integer id,@PathVariable Integer choiceState ){
        return questionsService.modifyByChoiceState(id,choiceState);
    }


    /**
     * 试题审核
     */
    @PostMapping("/check/{id}")
    public ResponseResult modifyByChkstate(@PathVariable Integer id,@RequestBody HmQuestions hmQuestions){
        String cookie = request.getHeader("Authorization");
        String[] split = cookie.split(" ");
        String s = split[1];
        return questionsService.modifyByChkstate(id,hmQuestions,s);
    }
}
